//
//  BrandDetailViewController.m
//  一键车讯
//
//  Created by 江玉元 on 14/10/29.
//  Copyright (c) 2014年 lanou.3g.com. All rights reserved.
//

#import "BrandDetailViewController.h"
#import "CarDetailViewController.h"
#import "NetHandler.h"
#import "BrandDetail.h"
#import "BrandDetailCell.h"
@interface BrandDetailViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, retain)NSMutableArray *arraySection;
@property (nonatomic, retain)NSMutableArray *arrayRow;
@property (nonatomic, retain)NSMutableDictionary *dictionary;
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSString *carTitle;
@end

@implementation BrandDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.array = [NSMutableArray array];
        self.arraySection = [NSMutableArray array];
        self.dictionary = [NSMutableDictionary dictionary];
    }
    return self;
}

- (void)startConnection
{
    NSString *url = [NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/v3/price/getSerialListByBrandId/%@", self.brand.Id];
    [NetHandler networkWithURL:url completion:^(id result) {
        NSMutableArray *dicArr = [result objectForKey:@"manufacturers"];
        self.array = dicArr;
        for (NSDictionary *dic in self.array) {
            [self.arraySection addObject:[dic objectForKey:@"name"]];
            self.title = [dic objectForKey:@"brandName"];
        }
        for (int i = 0; i < self.arraySection.count; i++) {
            self.arrayRow = [NSMutableArray array];
            for (NSDictionary *brandDic in [[self.array objectAtIndex:i] objectForKey:@"serials"]) {
                BrandDetail *brandDetail = [[BrandDetail alloc] initWithDictionary:brandDic];
                [self.arrayRow addObject:brandDetail];
            }
            [self.dictionary setValue:self.arrayRow forKey:self.arraySection[i]];
        }
        [self.tableView reloadData];
    }];
}

- (void)setupView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.tableView];
    [_tableView release];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.carTitle;
    [self startConnection];
    [self setupView];
}

- (void)dealloc
{
    [_array release];
    [_arrayRow release];
    [_arraySection release];
    [_dictionary release];
    [_tableView release];
    [_carTitle release];
    [super dealloc];
}

#pragma mark 协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.arraySection.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.arraySection[section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSMutableArray *arr = [self.dictionary objectForKey:[self.arraySection objectAtIndex:(section)]];
    return arr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"brand";
    BrandDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[[BrandDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str] autorelease];
    }
    NSMutableArray *arr = [self.dictionary objectForKey:[self.arraySection objectAtIndex:(indexPath.section)]];
    BrandDetail *brandDetail = arr[indexPath.row];
    cell.brandDetail = brandDetail;
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CarDetailViewController *carDetailVC = [[CarDetailViewController alloc] init];
    NSMutableArray *arr1 = [self.dictionary objectForKey:[self.arraySection objectAtIndex:(indexPath.section)]];
    carDetailVC.brandDetail = arr1[indexPath.row];
    [self.navigationController pushViewController:carDetailVC animated:YES];
    [carDetailVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
